package com.ahstu.learning.notepad;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ahstu.learning.R;

import java.util.ArrayList;


public class ListActivity extends Activity implements View.OnClickListener {
    ListView lvTitle;
    ImageView imageViewBack;
    TextView textViewBuild;
    final ArrayList<Integer> IdArray = new ArrayList<Integer>();
    static ListActivity list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);
        imageViewBack = (ImageView) findViewById(R.id.notepad_title_back);
        textViewBuild = (TextView) findViewById(R.id.buildNewNotepad);
        imageViewBack.setOnClickListener(this);
        textViewBuild.setOnClickListener(this);
        lvTitle = (ListView) findViewById(R.id.mylist);
        list = this;
        lvTitle.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(ListActivity.this, updataActivity.class);
                intent.putExtra("id", IdArray.get(position));

                startActivity(intent);
            }
        });


    }

    public void Initdata() {
        Database helper = new Database(ListActivity.this, "node.db", null, 1);
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cs = db.query("thing", new String[]{"id", "title"}, null, null, null, null, null);
        ArrayList<String> al = new ArrayList<String>();
        while (cs.moveToNext()) {
            int id = cs.getInt(cs.getColumnIndex("id"));
            String title = cs.getString(cs.getColumnIndex("title"));
            IdArray.add(id);
            al.add(title);

        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al);
        lvTitle.setAdapter(adapter);

    }


    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    //@Override
    protected void onResume() {
        super.onResume();
        Initdata();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.notepad_title_back:
                finish();
                break;
            case R.id.buildNewNotepad:
                startActivity(new Intent(ListActivity.this, NewActivity.class));
                break;
        }
    }
}
